package com.work.keep.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;

import com.work.keep.R;
import com.work.keep.activity.CollectionActivity;
import com.work.keep.activity.OnRecyclerItemClickListener;
import com.work.keep.activity.UserInforActivity;
import com.work.keep.adapter.UserInforAdapter;
import com.work.keep.base.BaseFragment;
import com.work.keep.ui.PullToRefreshBase;
import com.work.keep.ui.PullToRefreshRecyclerView;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by dllo on 16/1/21.
 */
public class UserInforFragment extends BaseFragment {
    private Context context;
    private PullToRefreshRecyclerView userInforRecycler;
    private RecyclerView mRecycler;
    private boolean mIsStart = true;
    private SimpleDateFormat mDateFormat = new SimpleDateFormat("MM-dd HH:mm");
    private UserInforAdapter userInforAdapter;
    private GridLayoutManager gridLayoutManager;
    private ImageView userInforLeft;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_user_infor, container, false);
        return v;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        userInforLeft = (ImageView) view.findViewById(R.id.userInforLeft);
        userInforRecycler = (PullToRefreshRecyclerView) view.findViewById(R.id.userInforRecycler);
        userInforRecycler.setPullLoadEnabled(true);
        userInforRecycler.setScrollLoadEnabled(false);
        mRecycler = (RecyclerView) userInforRecycler.getRefreshableView();
    }

    @Override
    protected void init() {
        userInforLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        userInforAdapter = new UserInforAdapter(context);
        gridLayoutManager = new GridLayoutManager(context, 3);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {

                if (position > 0 && position <= 3) {
                    return 1;
                }
                return 3;
            }
        });
        mRecycler.setLayoutManager(gridLayoutManager);
        mRecycler.setAdapter(userInforAdapter);
        userInforAdapter.setOnRecyclerItemClickListener(new OnRecyclerItemClickListener() {
            @Override
            public void onItemClick(int position) {
                switch (position) {
                    case 2:
                        Intent intent = new Intent(context, CollectionActivity.class);
                        context.startActivity(intent);
                        break;
                    case 3:
                        UserInforActivity.goToUserInforActivity(context);
                        break;
                }
            }
        });
        userInforRecycler.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                mIsStart = true;
                new GetDataTask().execute();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                mIsStart = false;
                new GetDataTask().execute();
            }
        });
    }

    private class GetDataTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            // Simulates a background job.
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            //回来可以在这里做判断
            boolean hasMoreData = true;
            if (mIsStart) {
//                demoAdapter.refresh();
            } else {
//                demoAdapter.loadMore();
            }
            userInforRecycler.onPullDownRefreshComplete();
            userInforRecycler.onPullUpRefreshComplete();
            userInforRecycler.setHasMoreData(hasMoreData);
            String text = formatDateTime(System.currentTimeMillis());
            userInforRecycler.setLastUpdatedLabel(text);
            super.onPostExecute(aVoid);

        }
    }

    private String formatDateTime(long time) {
        if (0 == time) {
            return "";
        }

        return mDateFormat.format(new Date(time));
    }
}
